package com.example.mobile2021606129ly0406;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private Button butMainCreat;
    private RadioGroup radGroMainLine;
    private RadioButton radButLine1;
    private RadioButton radButLine2;
    private RadioButton radButLine3;
    private RadioButton radButLine4;
    private TextView tvMainTitle;
    private RadioGroup radGroCarType;
    private RadioButton radBtnMainSuv;
    private RadioButton radButMainSaloon;
    private RadioButton radButMvp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();


        //给第一组单选项选中状态改变设置监听器
        radGroCarType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton=findViewById(checkedId);
                Log.i("RadioButton",radioButton.getText().toString());//把控件文本提取出来
            }
        });
        //5.第二组
        radGroMainLine.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int i) {
                RadioButton radioButton=findViewById(i);
                Log.i("RadioButton",radioButton.getText().toString());
            }
        });
        //6.给按钮设置监听器，完成单选项数据的获取
        butMainCreat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //1.radGroCarType.getCheckedRadioButtonId();//获取id
                String msg="";
                int carID = radGroCarType.getCheckedRadioButtonId();
                if (carID != -1) {
                    msg=((RadioButton)findViewById(carID)).getText().toString();
//                    RadioButton radioButton = findViewById(carID);
//                    Log.i("RadioButton", radioButton.getText().toString());
                } else {
                    msg="未选车型";
                }

                //2.RadioButton radioButton=findViewById(radGroCarType.getCheckedRadioButtonId());
                //  radioButton=null   getCheckedRadioButtonId()=-1
                //  空白选项时，程序系统会崩坏
                //  Log.i("RadioButton",radioButton.getText().toString());


                int lineId = radGroMainLine.getCheckedRadioButtonId();
                if (lineId != -1) {
                    msg += "," + ((RadioButton) findViewById(lineId)).getText().toString();
                } else {
                    msg += ",未选生产线";
                }
                Log.i("点击按钮", msg);
            }


        });
    }
//定义initView（）方法
    private void initView() {
        butMainCreat = (Button) findViewById(R.id.but_main_Creat);
        radGroMainLine = (RadioGroup) findViewById(R.id.radGro_main_line);
        radButLine1 = (RadioButton) findViewById(R.id.radBut_line1);
        radButLine2 = (RadioButton) findViewById(R.id.radBut_line2);
        radButLine3 = (RadioButton) findViewById(R.id.radBut_line3);
        radButLine4 = (RadioButton) findViewById(R.id.radBut_line4);
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        radGroCarType = (RadioGroup) findViewById(R.id.radGro_car_type);
        radBtnMainSuv = (RadioButton) findViewById(R.id.radBtn_main_suv);
        radButMainSaloon = (RadioButton) findViewById(R.id.radBut_main_saloon);
        radButMvp = (RadioButton) findViewById(R.id.radBut_mvp);
    }
}